﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using Public;
using DevExpress.XtraGrid.Views.Grid;
using System.Data.SqlClient;
using System.Data.Odbc;

namespace QuanLyKhoHangVatTu
{
    public partial class frmNhapkho : DevExpress.XtraEditors.XtraForm
    {
        NhapKho_BUS nk_bus = new NhapKho_BUS();
        NhapKho_PB nk_pb = new NhapKho_PB();

        public frmNhapkho()
        {
            InitializeComponent();
        }

        private void frmNhapkho_Load(object sender, EventArgs e)
        {
            cboDoiTuong.Properties.DataSource = nk_bus.LayDSNCC();
            cboDoiTuong.Properties.DisplayMember = "MANCC";
            cboDoiTuong.Properties.ValueMember = "TENNCC";

            colMaHang.DataSource = nk_bus.LayDSTonKho();
            colMaHang.DisplayMember = "MAVATTU";
            colMaHang.ValueMember = "MAVATTU";

            colKho.DataSource = nk_bus.LayDSKho();
            colKho.DisplayMember = "TENKHO";
            colKho.ValueMember = "MAKHO";

            gridControl1.DataSource = nk_bus.LayDSCTNhapKho();

            
            
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barButtonItem1_ItemClick_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void xtraTabPage1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        private void gridControl1_Click_1(object sender, EventArgs e)
        {

        }

        private void textEdit1_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void cboDoiTuong_EditValueChanged(object sender, EventArgs e)
        {
            txtTenDoiTuong.Text = cboDoiTuong.EditValue.ToString();
            
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            for (int i = gridView1.RowCount-1; i >=0; i--)
            {
                NhapKho_PB.ID = nk_bus.NextID();
                NhapKho_PB.MaPhieuNhap = txtMa.Text;
                NhapKho_PB.MaHang = gridView1.GetRowCellValue(i, "MAHANG").ToString();
                NhapKho_PB.MaKho = gridView1.GetRowCellValue(i, "MAKHO").ToString();
                NhapKho_PB.SoLuong = gridView1.GetRowCellValue(i, "SOLUONG").ToString();
                NhapKho_PB.ThanhTien = gridView1.GetRowCellValue(i, "THANHTIEN").ToString();
                NhapKho_PB.DonGia = gridView1.GetRowCellValue(i, "DONGIA").ToString();

                nk_bus.ThemCTNhapKho(nk_pb);
            }
            frmNhapkho_Load(sender, e);
        }
        #region "Lấy giá trị textbox"
        private NhapKho_PB GetValues()
        {
            NhapKho_PB.MaPhieuNhap = txtMa.Text;
            NhapKho_PB.MaNCC = cboDoiTuong.Text;
            NhapKho_PB.NguoiGiaoHang = txtNguoiGiaoHang.Text;
            NhapKho_PB.DienGiai = txtDienGiai.Text;
            NhapKho_PB.NgayHachToan = Convert.ToDateTime(dtHachToan.EditValue);
            NhapKho_PB.NgayChungTu = Convert.ToDateTime(dtChungTu.EditValue);
            NhapKho_PB.SoChungTu = txtSoChungTu.Text;
            return nk_pb;
        }
        #endregion

        private void menuThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            GetValues();
            nk_bus.ThemNhapKho(nk_pb);
        }

        private void dateEdit2_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void txtNguoiGiaoHang_TextChanged(object sender, EventArgs e)
        {

        }

        private void gridView1_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            // Sự kiện này để người ta không chuyển qua dòng khác được khi có lỗi xảy ra nè
            // Nó nhận giá trị e.Valid của gridView1_ValidateRow để ứng xử
            // neu e,Valid =True thì nó cho chuyển qua dòng khác hoặc làm tác vụ khác
            // và ngược lại
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
        }

        private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {

            //NhapKho_PB.ID = nk_bus.NextID();
            //NhapKho_PB.MaPhieuNhap = txtMa.Text;
            //NhapKho_PB.MaHang = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MAHANG").ToString();
            //NhapKho_PB.MaKho = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MAKHO").ToString();
            //NhapKho_PB.SoLuong = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "SOLUONG").ToString();
            //NhapKho_PB.ThanhTien = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "THANHTIEN").ToString();
            //NhapKho_PB.DonGia = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DONGIA").ToString();
            ////Kiểm tra đây là dòng dữ liệu mới hay cũ, nếu là mới thì mình insert
            //GridView view = sender as GridView;
            //if (view.IsNewItemRow(e.RowHandle))
            //    nk_bus.ThemCTNhapKho(nk_pb);
            ////ngược lại thì update
            //else
            //{
            //    NhapKho_PB.ID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID").ToString();
            //    nk_bus.SuaCTNhapKho(nk_pb);

            //}
            //frmNhapkho_Load(sender, e);
        }

        private void gridControl1_Click_2(object sender, EventArgs e)
        {

        }

        private void menuXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            for (int i = gridView1.GetSelectedRows().Length - 1; i >= 0; i--)
            {
                NhapKho_PB.ID= gridView1.GetRowCellValue(gridView1.GetSelectedRows()[i], "ID").ToString();
                nk_bus.XoaCTNhapKho(nk_pb);        
            }
            frmNhapkho_Load(sender, e);
        }

            
        }
    }